import csv

import requests
from lxml import etree

lst = []
for page in range(1,11):
    url = f'https://movie.douban.com/top250?start={(page-1)*25}&filter='

    head = {
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',
    }

    res = requests.get(url, headers=head)

    html = res.text
    tree = etree.HTML(html)

    item = tree.xpath('//div[@class="item"]')
    for i in item:
        dic = {}
        title = i.xpath('./div[@class="info"]/div[@class="hd"]//text()')
        titles = ''.join(title).replace('\n', '').replace(' ', '')
        dic['titles'] = titles
        direct = i.xpath('./div[@class="info"]/div[@class="bd"]/p/text()')[1]
        directs = direct.strip().split('/')[2]
        dic['directs'] = directs
        mark = i.xpath('./div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[2]/text()')[0]
        dic['mark'] = mark
        try:
            yinyan = i.xpath('./div[@class="info"]/div[@class="bd"]/p[@class="quote"]/span/text()')[0]
            dic['yinyan'] = yinyan
        except IndexError:
            dic['yinyan'] = ''
        # lianjie = i.xpath('./div[@class="info"]/div[@class="hd"]/a/@href')[0]
        lst.append(dic)

head = ('titles', 'directs', 'mark', 'yinyan')
with open("doubantop250.csv", 'w', encoding='utf-8-sig', newline='') as f:
    w = csv.DictWriter(f, fieldnames=head)
    w.writeheader()
    w.writerows(lst)